package com.system.user.service;


import com.system.common.bean.Admin;
import com.system.common.bean.User;
import com.system.common.service.IAdminService;
import com.system.common.service.ILoginService;
import com.system.common.service.IUserService;
import com.system.common.util.PasswordUtil;
import com.system.common.util.SYS;
import com.system.common.util.TokenUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;



@DubboService
public class LoginServiceImpl implements ILoginService {
    @DubboReference
    private IAdminService adminService;
    @DubboReference
    private IUserService userService;

    @Override
    public String userLogin(User user) {
        //登录逻辑
        //1.登录用户名查询数据库
        User result = userService.getUserByUsername(user.getUsername());
        if (result==null){
            throw new RuntimeException("登陆异常");
        }

        //2.验证密码
        String password = user.getPassword();
        String encryptionPassword = result.getPassword();
        if (!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(), "1");
        return token;
    }

    @Override
    public String adminLogin(Admin admin) {
        //登录逻辑
        //1.登录用户名查询数据库
        Admin result = adminService.getAdminByUsername(admin.getUsername());
        if (result==null){
            throw new RuntimeException("登陆异常");
        }

        //2.验证密码
        String password = admin.getPassword();
        String encryptionPassword = result.getPassword();
        if (!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(), "1");
        return token;
    }

    @Override
    public User loginUserInfo(String token) {
        try {
            // 验证token是否为空
            if (token == null || token.trim().isEmpty()) {
                throw new RuntimeException("登录凭证缺失");
            }

            // token中存储的用户id
            Object id = TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
            if (id == null) {
                throw new RuntimeException("登录凭证已过期或无效");
            }

            // 验证ID是否为有效数字
            Integer userId;
            try {
                userId = Integer.parseInt(id.toString());
            } catch (NumberFormatException e) {
                throw new RuntimeException("用户信息异常");
            }

            User user = userService.getUserById(userId);
            if (user == null) {
                throw new RuntimeException("用户不存在");
            }

            return user;
        } catch (Exception e) {
            // 捕获所有异常并转换为统一的错误信息
            if (e instanceof RuntimeException) {
                throw e; // 重新抛出已知异常
            } else {
                throw new RuntimeException("登录验证失败: " + e.getMessage());
            }
        }
    }


    @Override
    public Admin loginAdminInfo(String token) {
        try {
            // 验证token是否为空
            if (token == null || token.trim().isEmpty()) {
                throw new RuntimeException("登录凭证缺失");
            }

            // token中存储的用户id
            Object id = TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
            if (id == null) {
                throw new RuntimeException("登录凭证已过期或无效");
            }

            // 验证ID是否为有效数字
            Integer adminId;
            try {
                adminId = Integer.parseInt(id.toString());
            } catch (NumberFormatException e) {
                throw new RuntimeException("客户信息异常");
            }

            Admin admin = adminService.getAdminById(adminId);
            if (admin == null) {
                throw new RuntimeException("客户不存在");
            }

            return admin;
        } catch (Exception e) {
            // 捕获所有异常并转换为统一的错误信息
            if (e instanceof RuntimeException) {
                throw e; // 重新抛出已知异常
            } else {
                throw new RuntimeException("客户登录验证失败: " + e.getMessage());
            }
        }
    }

}

